package com.jiyinit.web.core.mobile.controller;

import com.jiyinit.core.framework.base.entity.ResponseDto;
import com.jiyinit.core.framework.base.util.JsonEntityTransform;
import com.jiyinit.core.framework.base.util.LogUtil;
import com.jiyinit.web.core.business.dto.ReadMessageRequest;
import com.jiyinit.web.core.business.dto.UserTaskRequest;
import com.jiyinit.web.core.business.entity.TabCoreNoticeEntity;
import com.jiyinit.web.core.business.entity.TabCoreTask;
import com.jiyinit.web.core.business.entity.TabSendMessageEntity;
import com.jiyinit.web.core.business.enums.SystemConstant;
import com.jiyinit.web.core.business.service.NoticeServiceImpl;
import com.jiyinit.web.core.business.service.ReadMessageServiceImpl;
import com.jiyinit.web.core.business.service.TaskServiceImpl;
import com.jiyinit.web.core.business.service.UserTaskServiceImpl;
import com.jiyinit.web.core.common.logic.ParamCheckLogic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户管理
 *
 * @author zhaolaiwei
 * @version 1.0
 * @date 2017/4/13 9:01
 * @since 1.0
 */
@Controller
@RequestMapping("/mobile/message")
public class MsendMessageController {

    /**
     * 日志对象
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(MsendMessageController.class);

    @Resource(name = "core.paramCheckLogic")
    private ParamCheckLogic paramCheckLogic;

    @Resource(name = "send.message")
    private ReadMessageServiceImpl readMessageService;

    @Resource(name = "core.taskServiceImpl")
    private TaskServiceImpl taskService;

    @Resource(name = "core.noticeServiceImpl")
    private NoticeServiceImpl noticeService;

    @Resource(name = "core.userTaskServiceImpl")
    private UserTaskServiceImpl userTaskServiceImpl;

//
//    /**
//     * 按条件查询用户信息
//     *
//     * @param request
//     * @param userRequest
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(method = RequestMethod.POST, value = "/findFunction")
//    public List<TabCoreFunEntity> findAll(HttpServletRequest request, UserRequest userRequest) {
//        List<TabCoreFunEntity> = userService.getUserInfoListByCondiction(userRequest);
//        LogUtil.info(LOGGER, "getTotalRecords:{},getTotalPages:{},getRows:{}", pagingResult.getTotalRecords(), pagingResult.getTotalPages(),pagingResult.getRows());
//        return pagingResult;
//    }

    /**
     * 根据任务ID userId 将未读的消息设为已读
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "readMessage")
    public ResponseEntity<ResponseDto> readMessage(HttpServletRequest request) {
        String logJson = request.getParameter("request_json");
        LogUtil.info(LOGGER, "logJson:{}", logJson);
        ReadMessageRequest readMessageRequest = (ReadMessageRequest) JsonEntityTransform.json2Object(logJson, ReadMessageRequest.class);
        //更新说说的状态为已读
        readMessageService.read(readMessageRequest);
        //更新用户参与任务的状态为已读
        UserTaskRequest userTaskRequest=new UserTaskRequest();
        userTaskRequest.setUserId(readMessageRequest.getUserId());
        userTaskRequest.setTaskId(readMessageRequest.getTaskId());
        userTaskServiceImpl.updateIsReadTaskStatus(userTaskRequest);

        return new ResponseEntity<>(ResponseDto.responseOK("成功！"), HttpStatus.OK);
    }

     /**
      * 更新日志说说状态
      * @author liuzongguang
      * @create 2018/8/20 11:38
      * @param
      * @return
      */
    @ResponseBody
    @RequestMapping(value = "readDailyLogMessage")
    public ResponseEntity<ResponseDto> readDailyLogMessage(HttpServletRequest request) {
        String logJson = request.getParameter("request_json");
        LogUtil.info(LOGGER, "logJson:{}", logJson);
        ReadMessageRequest readMessageRequest=JsonEntityTransform.json2Object(logJson, ReadMessageRequest.class);
        readMessageService.read(readMessageRequest);
        return new ResponseEntity<>(ResponseDto.responseOK("成功！"), HttpStatus.OK);
    }

    @ResponseBody
    @RequestMapping(value = "noReadMessage")
    public ResponseEntity<ResponseDto> noReadMessage(HttpServletRequest request) {
        String logJson = request.getParameter("request_json");
        LogUtil.info(LOGGER, "logJson:{}", logJson);
        ReadMessageRequest readMessageRequest = (ReadMessageRequest) JsonEntityTransform.json2Object(logJson, ReadMessageRequest.class);
        if (readMessageRequest.getUserId() == null) {
            return new ResponseEntity<>(ResponseDto.responseOK("用户ID不能为空"), HttpStatus.OK);
        }
        List<TabSendMessageEntity> list = readMessageService.findNoReadMessage(readMessageRequest.getUserId());

        List<TabSendMessageEntity> taskList = new ArrayList();
        List<TabSendMessageEntity> noticeList = new ArrayList();

        for (TabSendMessageEntity mess : list) {
            if (SystemConstant.NOTICE_BOARD.intValue() == mess.getFunId().intValue()) {
                //公告
                noticeList.add(mess);
            } else {
                taskList.add(mess);
            }
        }
        List<TabCoreTask> task = new ArrayList<>();
        List<TabCoreNoticeEntity> notice = new ArrayList<>();
        if(taskList != null && taskList.size() > 0){
            task = taskService.findTaskByIds(taskList);}
        if(noticeList != null && noticeList.size() > 0){
            notice = noticeService.findTaskByIds(noticeList);}

        //排序
        List<Object> result = new ArrayList<>();
        for (TabSendMessageEntity mess : list) {
            if (SystemConstant.NOTICE_BOARD.intValue() == mess.getFunId().intValue()) {
                for (TabCoreNoticeEntity no : notice) {
                    if (mess.getTaskId().intValue() == no.getNoticeId().intValue()) {
                        result.add(no);
                    }
                }
            } else {
                for (TabCoreTask t : task) {
                    if (mess.getTaskId().intValue() == t.getTaskId().intValue()) {
                        result.add(t);
                    }
                }
            }
        }
        return new ResponseEntity<>(ResponseDto.responseOK(result), HttpStatus.OK);
    }
}
